package com.bins.uaa.user.controller;

import com.bins.uaa.common.constants.ResponseBody;
import com.bins.uaa.user.model.User;
import com.bins.uaa.user.service.RoleService;
import com.bins.uaa.user.service.UserService;
import com.bins.uaa.user.utils.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerProperties;
import org.springframework.http.*;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/authorize")
public class ClientController {

//    @Autowired
//    private AuthorizationCodeResourceDetails authorizationCodeResourceDetails;
//
//    @Autowired
//    private ResourceServerProperties resourceServerProperties;
//
//    @Autowired
//    private RestTemplate restTemplate;
//
//    @Autowired
//    private BCryptPasswordEncoder passwordEncoder;
//
//    @Autowired
//    private UserService userService;
//
//    @Autowired
//    private RoleService roleService;
//
//    @RequestMapping("/login")
//    public ResponseBody login(String code, HttpServletRequest request, HttpServletResponse response) throws Exception {
//        if (StringUtils.isEmpty(code)) {
//            return ResponseBody.FAIL();
//        }
//        LinkedMultiValueMap<String, Object> valueMap = new LinkedMultiValueMap<>();
//        valueMap.add("client_id", authorizationCodeResourceDetails.getClientId());
//        valueMap.add("client_secret", authorizationCodeResourceDetails.getClientSecret());
//        valueMap.add("grant_type", authorizationCodeResourceDetails.getGrantType());
//        valueMap.add("redirect_uri", authorizationCodeResourceDetails.getPreEstablishedRedirectUri());
//        valueMap.add("code", code);
//        Map<String, String> map = HttpUtil.doFrom(authorizationCodeResourceDetails.getAccessTokenUri(), valueMap, Map.class);
//
//        Map principal = HttpUtil.doGet(resourceServerProperties.getUserInfoUri() + "?access_token=" + map.get("access_token"), Map.class);
//
//        String username = principal.get("name").toString();
//        if (userService.findByUsername(username) == null) {
//            // Role role = roleService.findByRoleType(Role.RoleType.USER);
//            User newUser = new User();
//            newUser.setUserName(username);
//            newUser.setPassword(passwordEncoder.encode(username));
//            // newUser.getRoles().add(role);
//            userService.save(newUser);
//        }
//        HttpHeaders httpHeaders = new HttpHeaders();
//        httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//        LinkedMultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
//        params.add("username", username);
//        params.add("password", username);
//        HttpEntity<LinkedMultiValueMap<String, ? extends Object>> httpEntity = new HttpEntity(params, httpHeaders);
//        String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/login";
//        ResponseEntity<Object> exchange = restTemplate.exchange(url, HttpMethod.POST, httpEntity, Object.class);
//        HttpHeaders headers = exchange.getHeaders();
//        for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
//            entry.getValue().stream().forEach(value -> response.addHeader(entry.getKey(), value));
//        }
//        response.setStatus(exchange.getStatusCode().value());
//        return ResponseBody.SUCCESS();
//    }
}
